const explain = require("explain")
const uniID = require('uni-id')

const OCR_ID_CARD_URL = 'https://dm-51.data.aliyun.com/rest/160601/ocr/ocr_idcard.json'
const OCR_BANK_CARD_URL = 'https://yhk.market.alicloudapi.com/rest/160601/ocr/ocr_bank_card.json'
const OCR_APPCODE = '3dd98e04ae4449a48a56335365e964d0'

module.exports = class ocr extends explain.service {
	async ocrIdCard(params) {
		const payload = await uniID.checkToken(params.token)
		if (payload.code) {
			return payload
		}
		try {
			return await uniCloud.httpclient.request(OCR_ID_CARD_URL, {
				method: 'POST',
				data: {
					image: params.url,
					configure: {
						side: params.side
					}
				},
				headers: {
					Authorization: `APPCODE ${OCR_APPCODE}`
				},
				contentType: 'json',
				dataType: 'json'
			})
		} catch (e) {
			return {
				data: {
					success: false
				}
			}
		}
	}

	async ocrBankCard(params) {
		const payload = await uniID.checkToken(params.token)
		if (payload.code) {
			return payload
		}
		try {
			return await uniCloud.httpclient.request(OCR_BANK_CARD_URL, {
				method: 'POST',
				data: {
					image: params.url,
				},
				headers: {
					Authorization: `APPCODE ${OCR_APPCODE}`
				},
				contentType: 'json',
				dataType: 'json'
			})
		} catch (e) {
			return {
				data: {
					success: false
				}
			}
		}
	}
}
